package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Newsupplierservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	Distributiondatabase ddb;
	public Newsupplierservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = new String(request.getParameter("type").getBytes("iso-8859-1"),"utf-8");
		if(type.equals("delete"))
		{
			String name=new String(request.getParameter("supplier_name").getBytes("iso-8859-1"),"utf-8");
	    	ddb.suppliersDelete(name);
		}
		else if(type.equals("insert"))
		{
			String name=new String(request.getParameter("supplier_name").getBytes("iso-8859-1"),"utf-8");
			String address=new String(request.getParameter("supplier_address").getBytes("iso-8859-1"),"utf-8");
			String linkman=new String(request.getParameter("supplier_linkman").getBytes("iso-8859-1"),"utf-8");
			String tel=new String(request.getParameter("supplier_tel").getBytes("iso-8859-1"),"utf-8");
			String bank_name=new String(request.getParameter("supplier_bank_name").getBytes("iso-8859-1"),"utf-8");
			String bank_account=new String(request.getParameter("supplier_bank_account").getBytes("iso-8859-1"),"utf-8");
			String fax=new String(request.getParameter("supplier_fax").getBytes("iso-8859-1"),"utf-8");
			String zipcode=new String(request.getParameter("supplier_zipcode").getBytes("iso-8859-1"),"utf-8");
			String corporation=new String(request.getParameter("supplier_corporation").getBytes("iso-8859-1"),"utf-8");
			String tips=new String(request.getParameter("supplier_tips").getBytes("iso-8859-1"),"utf-8");
			
	    	String update_name=new String(request.getParameter("update_name").getBytes("iso-8859-1"),"utf-8");
	    	   
	    	   if(update_name.equals("new")) 
	    	   {
	    		   System.out.println("insert new supplier");
	    		   ddb.suppliersInsert(name, address, linkman, tel, bank_name, bank_account, fax, zipcode, corporation, tips);
	    	   }
	    	   else
	    	   {
	    		   System.out.println("update supplier");
	    		   ddb.suppliersUpdate(name, address, linkman, tel, bank_name, bank_account, fax, zipcode, corporation, tips);
	    	   }				
		
		}else if(type.equals("update"))
	    {
			String name=new String(request.getParameter("supplier_name").getBytes("iso-8859-1"),"utf-8");
			System.out.println("update name="+name);
	    	ResultSet temprs=ddb.suppliersQuery(name);
	    	Supplier sup=new Supplier();
			try {
				if(temprs.next())
				{
					sup.setName(temprs.getString(1));
					sup.setAddress(temprs.getString(2));
					sup.setLinkman(temprs.getString(3));
					sup.setTel(temprs.getString(4));
					sup.setBank_name(temprs.getString(5));
					sup.setBank_account(temprs.getString(6));
					sup.setFax(temprs.getString(7));
					sup.setZipcode(temprs.getString(8));
					sup.setCorporation(temprs.getString(9));
					sup.setTips(temprs.getString(10));
					System.out.println(sup.toString());					   
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("updatesupplier", sup);
	    }
		
		ArrayList<Supplier> suppliers=new ArrayList<Supplier>();
		ResultSet rs=ddb.suppliersQuery();
		try {
				while(rs.next())
				{
					Supplier sup=new Supplier();
					sup.setName(rs.getString(1));
					sup.setAddress(rs.getString(2));
					sup.setLinkman(rs.getString(3));
					sup.setTel(rs.getString(4));
					sup.setBank_name(rs.getString(5));
					sup.setBank_account(rs.getString(6));
					sup.setFax(rs.getString(7));
					sup.setZipcode(rs.getString(8));
					sup.setCorporation(rs.getString(9));
					sup.setTips(rs.getString(10));
					suppliers.add(sup);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       request.setAttribute("suppliers", suppliers);
	       
	       if(type.equals("update"))
	       {
	    	   System.out.println("goto newsupplier.jsp");
	    	   request.getRequestDispatcher("../distribution/Newsupplier.jsp").forward(request, response); 
	       }
	       else 
	       {
	    	   System.out.println("goto querysupplier.jsp");
		       request.getRequestDispatcher("../distribution/Querysupplier.jsp").forward(request, response);  
	       }	       
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		ddb=new Distributiondatabase();
	}

}
